package top.stmo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 *      请求转发:一种在服务器内部的资源跳转方式
 *
 *      请求转发的实现方式：
 *          req.getRequestDispatcher("资源B路径").forward(req,resp);
 *
 *      请求转发资源间共享数据:使用Request对象
 *  void setAttribute(String name,Object o);    存储数据到request域[范围,数据是存储在request对象]中
 *  Object getAttribute(String name);           根据key获取值
 *  void removeAttribute(String name);          根据key删除该键值对
 *
 */
@WebServlet("/req4")
public class Request4 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                                            throws ServletException, IOException {

        System.out.println("Request4---------------------");

        //转发前存储数据
        req.setAttribute("msg",123);

        //请求转发
        req.getRequestDispatcher("/req5").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                                        throws ServletException, IOException {
       doGet(req,resp);
    }
}

